<!-- 生产数据趋势图 -->
<script lang="ts" setup>
import MyChart from '@/components/MyChart.vue'
import { ref, watch } from 'vue'
const props = defineProps(['data'])

const options = ref({
  grid: {
    top: 40,
    bottom: 35,
    left: 60,
    right: 20
  },
  tooltip: {
    trigger: 'axis',
    confine: true,
    axisPointer: {
      type: 'shadow',
      crossStyle: {
        color: '#999'
      }
    }
  },
  legend: {
    show: true,
    right: 20,
    icon: 'rect',
    itemWidth: 15,
    itemHeight: 13,
    // itemGap: 97,
    textStyle: {
      color: '#ebeef5',
      fontSize: 20
    },
    data: ['', '']
  },
  xAxis: [
    {
      type: 'category',
      data: [],
      axisPointer: {
        type: 'shadow'
      },
      axisLabel: {
        color: '#ebeef5',
        fontSize: 16,
        interval: 0,
        rotate: 30
      },
      axisTick: {
        show: false
      },
      axisLine: {
        show: true,
        lineStyle: {
          color: '#182758',
          width: 2
        }
      },
      splitLine: {
        show: false
      }
    }
  ],
  yAxis: [
    {
      type: 'value',
      name: '台',
      nameTextStyle: {
        color: '#ebeef5',
        fontSize: 16,
        align: 'right',
        padding: [0, 5, 8, 0]
      },
      axisLabel: {
        formatter: '{value}',
        color: '#ebeef5',
        fontSize: 16,
        align: 'right'
      },
      axisLine: {
        show: false,
        lineStyle: {
          color: '#182758',
          width: 2
        }
      },
      splitLine: {
        show: true,
        lineStyle: {
          color: '#182758',
          width: 1
        }
      }
    },
    {
      type: 'value',
      nameTextStyle: {
        color: '#ebeef5'
      },
      axisLabel: {
        show: false,
        formatter: '{value}',
        color: '#ebeef5',
        fontSize: 16
      },
      axisLine: {
        show: false
      },
      splitLine: {
        show: false
      }
    }
  ],
  series: [
    {
      name: '',
      type: 'bar',
      stack: 'total',
      barWidth: 20,
      color: '#00B8A9',
      tooltip: {
        valueFormatter: function (value: any) {
          return value + ''
        }
      },
      data: [],
      itemStyle: {
        //圆角
        // borderRadius: [30, 30, 30, 30]
      }
    },
    {
      name: '',
      type: 'bar',
      stack: 'total',
      barWidth: 20,
      color: '#F7C873',
      tooltip: {
        valueFormatter: function (value: any) {
          return value + ''
        }
      },
      label: {
        show: true,
        position: 'top',
        // 自定义标签内容，添加百分比符号
        formatter: (params: any)=> {
              // 获取当前系列和同一位置的其他系列数据
              const dataIndex = params.dataIndex;
              let sum = 0;
              
              // 遍历所有系列，累加同一数据点的值
              for (let i = 0; i < options.value.series.length; i++) {
                const series = options.value.series[i];
                if (series.stack === 'total' && series.data[dataIndex] !== undefined) {
                  sum += Number(series.data[dataIndex]);
                }
              }
              
              return sum;
            },
        color: '#fff',
        fontSize: 16
      },
      data: []
    }
  ]
})

watch(
  () => props.data,
  (val) => {
    const {legendList, timeList, okList, ngList } = val
    options.value.xAxis[0].data = timeList
    options.value.legend.data = legendList
    options.value.series[0].data = okList
    options.value.series[1].data = ngList
    options.value.series[0].name = legendList[0]
    options.value.series[1].name = legendList[1]
  },
  {
    deep: true, // 开启深度监听
    immediate: true // 立即触发一次（可选）
  }
)
</script>
<template>
  <MyChart height="200px" :option="options" />
</template>
